$(document).ready(function() {
	$('#enterprise').dataTable({
		"bProcessing" : true, //DataTables载入数据时，是否显示‘进度’提示
		"bServerSide" : true, //是否启动服务器端数据导入
		"bStateSave" : false, //是否打开客户端状态记录功能,此功能在ajax刷新纪录的时候不会将个性化设定回复为初始化状态
		"bJQueryUI" : true, //是否使用 jQury的UI theme
		"aLengthMenu" : [5, 10, 15], //更改显示记录数选项
		"iDisplayLength" : 10, //默认显示的记录数
		"bAutoWidth" : true, //是否自适应宽度 update
		"bScrollCollapse" : false, //是否开启DataTables的高度自适应，当数据条数不够分页数据条数的时候，插件高度是否随数据条数而改变
		"bPaginate" : true, //是否显示（应用）分页器
		"bInfo" : true, //是否显示页脚信息，DataTables插件左下角显示记录数
		"sPaginationType" : "full_numbers", //详细分页组，可以支持直接跳转到某页
		"bSort" : true, //是否启动各个字段的排序功能
		"aaSorting" : [[4, "ASC"]], //默认的排序方式，第3列，升序排列
		"bFilter" : false, //是否启动过滤、搜索功能
		"sPaginationType": "full_numbers",
		"sAjaxSource":parent.basePath+"sys/LoanExt/listRepaymentDetail",
		"fnServerData":retrieveData,  //自定义数据获取函数
		"aoColumns" : [ {
			"mDataProp" : "loanCode",
			"sTitle" : "贷款申请号",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender": function (data, display, row) {
				return '<a href="javascript:void(0);" onclick="loan_query('+row.loanId+','+row.clientId+');" class="tablelink" >'+data+'</a>';
			}
		}, {
			"mDataProp" : "contractCode",
			"sTitle" : "合同编号",
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender": function (data, display, row) {
				return '<a href="javascript:void(0);" onclick="contract_query('+row.loanId+');" class="tablelink" >'+data+'</a>';
			}
		}, {
			"mDataProp" : "loanBorrower",
			"sTitle" : "借款人",
			"sDefaultContent" : "",
			"sClass" : "center"
		}, {
			"mDataProp" : "clientCardId",
			"sTitle" : "证件号码",
			"sDefaultContent" : "",
			"sClass" : "center"
		}, {
			"mDataProp" : "repayEndDate",
			"sTitle" : "还款日期",
			"sDefaultContent" : "",
			"bSortable": true,
			"sClass" : "center",
			"mRender": function (data, display, row) {
				var value = '';
				if(_is_empty(data)){
					value = '';
				}else{
					value = new Date(data).format('yyyy-MM-dd');
				}
				
	            return value;
		     }
		}, {
			"mDataProp" : "deadlineNumber/deadlineTotal",
			"sTitle" : "还款期数/总期数",
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender": function (data, display, row) {
				var deadlineReal = row.deadlineNumber,deadlineTotal=row.deadlineTotal;
				return deadlineReal+'/'+deadlineTotal;
			}
		}, {
			"mDataProp" : "planCapital",
			"sTitle" : "应收本金（元）",
			"sDefaultContent" : "",
			"sClass" : "center"
		}, {
			"mDataProp" : "planInterest",
			"sTitle" : "应收利息（元）",
			"sDefaultContent" : "",
			"sClass" : "center"
		}, {
			"mDataProp" : "repayTotal",
			"sTitle" : "收款金额（元）",
			"sDefaultContent" : "",
			"sClass" : "center"
		},{
			"mDataProp" : "overdueStatus",
			"sTitle" : "逾期状态",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender": function (data, display, row) {	
				var returnStr = (data==1?"(正常)":"(逾期)");
				return returnStr;
			}
		},
		{
			"mDataProp" : "uid",
			"sTitle" : "操作",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender": function (data, display, row) {
				var id=row.id,loanId=row.loanId,repaymentId=row.repaymentId,repayStatus=row.repayStatus;
				if(repayStatus ==2 || repayStatus ==3 || repayStatus ==4){
					return parent._set_default_href([id,loanId,repaymentId],[{'title':'收款','func':'receivables'},{'title':'催收记录','func':'collection_records'}]);
				}else{
					return parent._set_default_href([id,loanId,repaymentId],[{'title':'催收记录','func':'collection_records'}]);
					//return parent._set_default_href([id,loanId,repaymentId],[{'title':'查看','func':'query'},{'title':'催收记录','func':'collection_records'}]);
				}
			}
		}
		],
		"oLanguage": {
			"sUrl":parent.basePath+"static_resources/language/lang-cn.txt" //这个是国际化，也就是汉化的各种文字数据源，写对url就行了
		}
	});
	//查询事件
	$('input[name="query_btn"]').click(function(){
		$('#enterprise').dataTable().fnDraw();
	});

});

var receivables = function(id,loanId,repaymentId){
	parent._do_goto_right('jsp/loan/loan_repayment_detail_edit.jsp?id='+id+'&loanId='+loanId+'&repaymentId='+repaymentId);
};

var query = function(id,loanId,repaymentId){
	parent._do_goto_right(parent.basePath+'jsp/loan/loan_repayment_detail.jsp?id='+id+'&loanId='+loanId+'&repaymentId='+repaymentId);
};

var collection_records = function(id,loanId){
	parent._do_goto_right(parent.basePath+'jsp/loan/loan_collection_result_detail.jsp?id='+id+'&loanId='+loanId);
};

var contract_query = function(id){
	parent._do_goto_right('jsp/loan/loan_contract_detail.jsp?loanId='+id);
};

var loan_query = function(id,clientId){
	parent._do_goto_right(parent.basePath+'jsp/loan/loan_detail.jsp?id='+id+'&clientId='+clientId);
};

//自定义数据获取函数
function retrieveData( sSource, aoData, fnCallback ) {
	var array = $("#_form_search_default").serializeArray();
	aoData = parent._set_query_conditions(aoData,array);
	$.ajax( {
		type: "POST", 
		url: sSource, 
		cache : false,
		dataType : "json",
		data : aoData, 
		success: function(resp) {
			if(resp && resp.state == 200){
				fnCallback(resp.datagrid);
				return false;
			}
			fnCallback(null);
		}
	});
}